package com.module.app.emoji.pop

import android.content.Context
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.module.app.emoji.view.TTEmojiFragment
import com.module.app.ext.emojiDelBtnFlow
import com.module.app.ext.emojiSendBtnFlow
import com.module.base.utils.DisplayUtils
import com.module.base.utils.ViewPager2Helper
import com.module.base.widget.pager.MyFragmentAdapter
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.KeyboardUtils
import com.module.app.R
import com.module.frame.ext.observe
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView

/**
 * @author bsnl_yanxuwen
 * @date 2020/11/4 18:19
 * Description :
 * 表情弹窗
 */
open class EmojiPop(context: Context, editText: EditText) : BottomPopupView(context) {

    companion object {
        open fun create(
            context: Context, height: Int, editText: EditText
        ): EmojiPop {
            return XPopup.Builder(context)
                .hasShadowBg(false)
                .maxHeight(DisplayUtils.dip2px(50f) + height)
                .isRequestFocus(false)
                .isViewMode(true)
                .moveUpToKeyboard(false)
                .asCustom(EmojiPop(context, editText)) as EmojiPop
        }
    }


    private lateinit var layout_head: View
    private lateinit var iv_emoji: ImageView
    private lateinit var viewPager: ViewPager2
    private lateinit var magicIndicator: MagicIndicator

    private val fragmentList: MutableList<Fragment> = ArrayList()
    private val titles: MutableList<String> = ArrayList()
    private lateinit var commonNavigator: CommonNavigator
    private val defaultPosition = 0
    private val editText: EditText = editText

    //是否要显示表情
    var isShowEmoji = false

    interface Listener {
        fun onSendEmoji(bean: com.module.app.emoji.bean.Emoji?)
        fun onDel()
    }

    private var listener: Listener? = null
    fun setListener(listener: Listener) {
        this.listener = listener
    }

    fun removeListener() {
        listener = null
    }

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_emoji
    }

    override fun onCreate() {
        super.onCreate()
        layout_head = findViewById(R.id.layout_head)
        viewPager = findViewById(R.id.viewPager)
        magicIndicator = findViewById(R.id.indicator)
        iv_emoji = findViewById(R.id.iv_emoji)
        iv_emoji.setOnClickListener {
            onClickEmoji()
        }
        bottomPopupContainer.enableDrag(false) //取消拖曳
        fragmentList.add(TTEmojiFragment())
        titles.add("头条")
        isShowEmoji = false
        if (isShowEmoji) {
            iv_emoji.setImageResource(R.drawable.app_icon_soft_key_board)
        } else {
            iv_emoji.setImageResource(R.drawable.app_icon_emoji)
        }
        initViewPage()
        initListener()
    }

    private fun initViewPage() {
        val adapter = MyFragmentAdapter((context as FragmentActivity), fragmentList)
        viewPager.adapter = adapter
        viewPager.offscreenPageLimit = fragmentList.size
        commonNavigator = CommonNavigator(context)
        commonNavigator.isAdjustMode = false
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return fragmentList.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val clipPagerTitleView = ClipPagerTitleView(getContext())
                clipPagerTitleView.text = titles[index]
                clipPagerTitleView.textSize = DisplayUtils.dip2px(14f).toFloat()
                clipPagerTitleView.textColor = getContext().resources.getColor(R.color.bg_gray_80)
                clipPagerTitleView.clipColor = getContext().resources.getColor(R.color.bg_default)
                clipPagerTitleView.setOnClickListener { viewPager.currentItem = index }
                return clipPagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                return null
            }
        }
        magicIndicator.navigator = commonNavigator
        ViewPager2Helper.bind(magicIndicator, viewPager)
        viewPager.setCurrentItem(defaultPosition, false)
        magicIndicator.visibility = GONE
    }

    protected fun initListener() {
        //表情发送监听
        observe(lifecycleScope, this, emojiSendBtnFlow) {
            listener?.onSendEmoji(it)
        }
        //删除按钮监听
        observe(lifecycleScope, this, emojiDelBtnFlow) {
            if (it) {
                listener?.onDel()
            }
        }
    }

    override fun dismiss() {
        super.dismiss()
        KeyboardUtils.hideSoftInput(editText)
        isShowEmoji = false
        if (isShowEmoji) {
            iv_emoji.setImageResource(R.drawable.app_icon_soft_key_board)
        } else {
            iv_emoji.setImageResource(R.drawable.app_icon_emoji)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    fun onClickEmoji() {
        isShowEmoji = !isShowEmoji
        if (isShowEmoji) {
            iv_emoji.setImageResource(R.drawable.app_icon_soft_key_board)
            KeyboardUtils.hideSoftInput(editText)
        } else {
            iv_emoji.setImageResource(R.drawable.app_icon_emoji)
            KeyboardUtils.showSoftInput(editText)
        }
    }

    override fun destroy() {
        removeListener()
    }
}